<?php

namespace app\models\system;

use Yii;

/**
 * This is the model class for table "set_sms".
 *
 * @property int $id
 * @property string $key_id
 * @property string $key_secret
 * @property string $sign
 * @property string $template
 * @property int $timeout 过期时间,分钟
 * @property int $switch 是否开启假验证码
 * @property int $len
 */
class SetSms extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'set_sms';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['timeout', 'switch'], 'integer'],
            [['key_id'], 'string', 'max' => 200],
            [['key_secret', 'sign', 'template'], 'string', 'max' => 255],

            // verify code length
            [['len'], 'safe'],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'key_id' => 'Key ID',
            'key_secret' => 'Key Secret',
            'sign' => 'Sign',
            'template' => 'Template',
            'timeout' => 'Timeout',
            'switch' => 'Switch',
        ];
    }

    public static function getLastOne()
    {
        $model = self::find()
            ->orderBy(['id' => SORT_DESC])
            ->one();

        if (!$model) {
            $model = new self();
        }

        return $model;
    }

    public static function saveData($data)
    {
        $model = self::getLastOne();

        if ($model->load(['SetSms' => $data]) && $model->save()) {
            return true;
        }

        var_dump($model->getErrors());
    }
}
